//implement ACEThreadImpl
//andycai_sc 2011-05-12

#include "GParallel/GAce/ACEThreadImpl.h"
#include <ace/ACE.h>

ACEThreadImpl::ACEThreadImpl(GThread* _thread) : GThreadImpl(_thread), m_thId(0)
{
}

ACEThreadImpl::~ACEThreadImpl()
{
}

void ACEThreadImpl::startImpl()
{
    m_threadMgr.spawn(thread_fuc,
                      static_cast<void*>(this),
                      THR_NEW_LWP | THR_JOINABLE | THR_INHERIT_SCHED,
                      &m_thId);
}

void ACEThreadImpl::stopImpl()
{
    if (m_thId != 0)
    {
        m_threadMgr.cancel(m_thId);
        m_thId = 0;
    }
}

void ACEThreadImpl::joinImpl()
{
    m_threadMgr.join(m_thId);
}

void ACEThreadImpl::ace_msleep(long mseconds)
{
    ACE_OS::sleep(ACE_Time_Value(0, mseconds * 1000));
}

#if (defined(USE_ACE_ALL) && !defined(NO_ACE_THREAD)) || defined(USE_ACE_THREAD)
GThreadImpl::sleepImpl* GThreadImpl::_sleepImpl = &(ACEThreadImpl::ace_msleep);
#endif

ACE_THR_FUNC_RETURN ACEThreadImpl::thread_fuc(void* arg)
{
    ACEThreadImpl* threadImpl = static_cast<ACEThreadImpl*>(arg);
    threadImpl->exec();
    return 0;
}

GThreadImpl* createThreadFromAce(GThread *_thread)
{
	return new ACEThreadImpl(_thread);
}
